<?php

namespace app\openapi\controller;

use app\openapi\ApiBaseController;
use app\openapi\validator\GetPaymentValidator;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Cache;

class Payment extends ApiBaseController
{
    /**
     * 获取支付状态
     * @return \think\response\Json
     */
    public function get()
    {
        try {
            $params = $this->request->param();

            $result = $this->validate($params, GetPaymentValidator::class);
            if(true !== $result) {
                throw new ValidateException($result);
            }

            $redis = Cache::store('redis')->handler();
            $orderStateKey = implode(':', ['order_state', $params['order_num']]);
            $orderActive = $redis->get($orderStateKey);

            $hash = '';
            if(-1 != $orderActive) {
                $hash = $orderActive;
                $orderActive = 1;
            }

            return $this->jsonSuccess('查询成功', ['status' => $orderActive, 'hash' => $hash]);
        }catch (Exception $e) {
            return $this->jsonError($e->getMessage());
        }catch (ValidateException $e) {
            return $this->jsonError($e->getMessage());
        }
    }
}